<?php

if (isset($_GET['action']) && $_GET['action'] == "signup") {

	$raid_id = abs($_GET['rid']);
	$status = abs($_GET['s']);

	$raid_count = $signup->count($raid_id, 0, -1, $_SESSION['uid']);
	
	

	$player_active = $player->get(0,"", 1, $_SESSION['uid']);

	$active_char = $player_active[0];

	$general_role_count = $signup->count($raid_id, 0, -1, 0, 0, $active_char['role_id'], -1);
	
	$class_role_count = $signup->count($raid_id, 0, -1, 0, 0, -1, $active_char['class_role_id']);

	// Get max signup values for the players raid roles
	$class_role_max = $raid_role->get($raid_id, $active_char['class_role_id']);
	
	$general_role_max = $raid_role->get($raid_id, -1, -1, $active_char['role_id']);
	// endget	
	
	$general_role_count = $general_role_count[0][0];
	
	$class_role_count = $class_role_count[0][0];

	//check for existing signups that are mains or subs and not removed (incase someone gets funny ;p)
	if ($raid_count[0][0] == 0) {

		$signup_raid = $raid->get($raid_id);

		$signup_raid = $signup_raid[0];

		$smarty->assign('class_role_max', $class_role_max[0]);
		
		$smarty->assign('general_role_max', $general_role_max[0]);
		
		$smarty->assign('general_role_count', $general_role_count);
		
		$smarty->assign('class_role_count', $class_role_count);
		
		$smarty->assign('signup_raid', $signup_raid);

		$smarty->assign('status', $status);

		$smarty->assign('active_char', $active_char);

		$smarty->display('signup.tpl', $_SESSION['uid']);


	}

} elseif (isset($_POST['action']) && $_POST['action'] == "signup") {


	$raid_id = abs($_POST['rid']);
	$type = $_POST['type'];
	$comment = $_POST['comment'];
	
	$raid_role_id = $_POST['raid_role'];

	if ($type == "main") {
		$type = 0;
	} elseif ($type == "sub") {
		$type = 1;
	}

	$chosen_role_count = $signup->count($raid_id, 0, -1, 0, 0,-1, -1, $raid_role_id);
	
	$max_chosen_role_count = $raid_role->get(0, 0, $raid_role_id);
	
	$chosen_role_count = $chosen_role_count[0][0];
	

	$max_chosen_role_count = $max_chosen_role_count[0];
	
	echo "<pre>";
	print_r($chosen_role_count);
	echo "</pre>";
	
	$signup_raid = $raid->get($raid_id);

	$signup_raid = $signup_raid[0];

	$raid_count = $signup->count($raid_id, 0, 0, $_SESSION['uid']);

	$unsub_count = $signup->count($raid_id, 1, -1, $_SESSION['uid']);

	$player_active = $player->get(0,"", 1, $_SESSION['uid']);

	$active_char = $player_active[0];

	$class_count = $signup->count($raid_id, 0, 0, 0, $active_char['class_id']);

	$total_count = $signup->count($raid_id, 0, 0);

	$total_count = $total_count[0][0];

	//check for existing signups that are mains or subs and not removed (incase someone gets funny ;p)
	
	if (($total_count < $signup_raid['no_players']) || $type == 1) {

		if ($raid_count[0][0] == 0) {

			$signup_raid = $raid->get($raid_id);

			$signup_raid = $signup_raid[0];


			if ($type == 0 && $active_char['team_order'] < $signup_raid['main_team_order']) {
			    $smarty->assign('message_type', "Error!");
				$smarty->assign('message', "You are not allowed to signup as a Main on this raid. <br>Dont cheat now!.");
				$smarty->assign('comment', $comment);
				$smarty->assign('status', 4);
			} elseif ($type == 1 && $active_char['team_order'] < $signup_raid['sub_team_order']) {
			    $smarty->assign('message_type', "Error!");
				$smarty->assign('message', "You are not allowed to signup as a Sub on this raid. <br>Dont cheat now!.");
				$smarty->assign('comment', $comment);
				$smarty->assign('status', 5);
			} elseif ($type == 0 && $chosen_role_count >= $max_chosen_role_count['max_main']) {
			    $smarty->assign('message_type', "Error!");
				$smarty->assign('errormessage', "It seems the raid role limit for this raid has been reached. <br>You can only sign up as a sub now.");
				$smarty->assign('comment', $comment);
				$smarty->assign('status', 4);
			} else {
				
				if($type == 1)
				{
					$raid_role_id = 0;
				}				
				
				if($unsub_count[0][0] == 0) {
					$signup->save(0, $active_char['player_id'], $raid_id, $type, 0, 0, 0, $comment, $raid_role_id);
					$smarty->assign('message', "You have been signed up for the raid.<br>Click on <a href=\"./index.php?page=raids\">Raids</a> to return to the raid page.");
					$smarty->assign('message_type', "Succesfull!");
				} else {
					$old_signup = $signup->get($raid_id, -1, -1, $_SESSION['uid']);

					$signup->save($old_signup[0]['turnin_id'], $active_char['player_id'], $raid_id, $type, 0, 0, 0, $comment, $raid_role_id);
					$smarty->assign('message_type', "Succesfull!");
					$smarty->assign('message', "You have been signed up for the raid.<br>Click on <a href=\"./index.php?page=raids\">Raids</a> to return to the raid page.");

				}



			}

		}

	} else {

		$smarty->assign('message', "The raid is full, you cannot sign up anymore. Please sign up as a sub!.");
		$smarty->assign('message_type', "Error!");
		$smarty->assign('comment', $comment);
		$smarty->assign('status', 4);

		

	$general_role_count = $signup->count($raid_id, 0, -1, 0, 0, $active_char['role_id'], -1);
	
	$class_role_count = $signup->count($raid_id, 0, -1, 0, 0, -1, $active_char['class_role_id']);
	
	$raid_roles = $raid_role->get($raid_id);
	
	$general_role_count = $general_role_count[0][0];
	
	$class_role_count = $class_role_count[0][0];
	
	$smarty->assign('raid_roles', $raid_roles);
	
	$smarty->assign('general_role_count', $general_role_count);
	
	$smarty->assign('class_role_count', $class_role_count);
		
	}

	
	$smarty->assign('signup_raid', $signup_raid);

	$smarty->assign('active_char', $active_char);

	$smarty->display('signup.tpl', $_SESSION['uid']);
}

?> 